package com.baizhi.serviceImpl;

import com.baizhi.dao.VideoDao;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.util.AliyunOSSUtil;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service("videoService")
@Transactional
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements VideoService {
    private String fileName = null;
    @Autowired
    private VideoDao videoDao;

    @Override
    public CommonVO page(PageDTO pageDTO) {
        //查所有
        IPage<Video> page = new Page<>(pageDTO.getPage() , pageDTO.getPageSize());
        IPage<Video> iPage = videoDao.selectPage(page, null);
        long total = iPage.getTotal();
        List<Video> records = iPage.getRecords();
        CommonVO commonVO = new CommonVO(pageDTO.getPage(), total,records);
        return commonVO;
    }

    /**
     * 添加视频
     *
     * @param video
     */
    @Override
    public CommonMessageVO add(Video video) {
        try {
            String path =" https://yingx20210827.oss-cn-beijing.aliyuncs.com/";
            String videoPath = path+fileName;
            video.setVideoPath(videoPath);
            int insert = videoDao.insert(video);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    /**
     * 文件上传至阿里云
     *
     * @param videoFile
     */
    @Override
    public String uploadHeadImg(MultipartFile videoFile) {
        String contentType = videoFile.getContentType();
        System.out.println("contentType = " + contentType);
        String[] split = contentType.split("/");
        if (split[0].equals("image")){
            //该文件是图片
            fileName = split[0]+"/"+System.currentTimeMillis()+"_"+videoFile.getOriginalFilename();;
        }if (split[0].equals("video")){
            //该文件是视频
            fileName = split[0]+"/"+System.currentTimeMillis()+"_"+videoFile.getOriginalFilename();;
        }
        AliyunOSSUtil.uploadfileBytes("yingx20210827",fileName,videoFile);
        return fileName;
    }

    /**
     * 根据id查
     *
     * @param video
     */
    @Override
    public Video queryById(Video video) {
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getId,video.getId());
        Video videos = videoDao.selectOne(queryWrapper);
        return videos;
    }

    @Override
    public CommonMessageVO update(Video video) {
        try {
            int i = videoDao.updateById(video);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonMessageVO delete(Video video) {
        try {
            /**
             * 删除前根据传递过来的id查一个，再获取地址进行删除
             */
            LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Video::getId,video.getId());
            Video videos = videoDao.selectOne(queryWrapper);
            System.out.println("videos = " + videos);
            String videoPath = videos.getVideoPath();
            System.out.println("videoPath = " + videoPath);
            /**
             * 字符串拆分
             */
            String path =" https://yingx20210827.oss-cn-beijing.aliyuncs.com/";
            String[] split = videoPath.split(path);
            for (String s : split) {
                System.out.println("s = " + s);
            }
            int i = videoDao.deleteById(video);
            AliyunOSSUtil.deleteBucket("yingx20210827",split[1]);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return CommonMessageVO.failed();
        }
    }
}